package cn.logger;

import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.Objects;

@Aspect
public class UserServiceLogger {
    private static final Logger logger = Logger.getLogger(UserServiceLogger.class);

    @Pointcut("execution(* cn.service.UserService.*(..))")
    public void pointcut(){}

    //前置增强代码
    @Before("pointcut()")
    public void before(JoinPoint joinPoint){
        logger.info("调用"+joinPoint.getTarget()+"的"+joinPoint.getSignature().getName()+"方法。方法入参:"+ Arrays.toString(joinPoint.getArgs()));
    }
    //后置增强代码
    @AfterReturning(pointcut = "pointcut()",returning = "result")
    public void afterReturning(JoinPoint joinPoint, Objects result){
        logger.info("调用"+joinPoint.getTarget()+"的"+joinPoint.getSignature().getName()+"方法。方法返回值:"+ result);
    }
}